// pages/askToBuyDetail/askToBuyDetail.js
const app = getApp();
import httpsUtils from '../../utils/https.js';
import myUtils from '../../utils/util.js';
import commonUtils from '../../utils/common.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    wanBuyId:'',
    indicatorDots: true,
    autoplay: true,
    interval: 3000,
    duration: 1000,
    circular: true,
    detailListImg: [],//图片
    datas: {},
    userData: {},
    isMy: false,//是否我的求购
    phone:'',
    isCall:false
  },
  /*发布*/
  askToBuyIssue: function (e) {
    //获取用户信息
    commonUtils.getUserInfoByToken(wx.getStorageSync('utoken'), 2, function () {
      wx.navigateTo({
        url: '../askToBuyIssue2/askToBuyIssue2?inIndex=false',
      })
    });
  },
  /**
   * 复制微信号码
   */
  cpWx:function(){
    var wxNum = this.data.datas.wx;
    if (wxNum == null || wxNum == ""){
      wx.showToast({
        title: '暂无微信号码',
        icon:"none"
      })
    }else{
      wx.setClipboardData({
        data: wxNum,
        success: function (res) {
          wx.showToast({
            title: '复制成功',
          })
        }
      })
    }
  },
  /**
   * 去聊天
   */
  toChat: function () {
    wx.showModal({
      title: '提示',
      content: '小程序暂不支持聊天功能,请下载服装基地app联系采购商',
    })
  },
  toindex: function () {
    var pages = getCurrentPages();
    if (pages.length >= 2 ) {
      wx.navigateBack({
        delta: 1
      })
    } else {
      wx.redirectTo({
        url: '../askToBuy/askToBuy',
        success: function () {
        },
      })
    }
  },
  /**
   * 预览
   */
  imgYu: function (event) {
    var src = event.currentTarget.dataset.src;//获取data-src
    var imgList = event.currentTarget.dataset.list;//获取data-list
    //图片预览
    wx.previewImage({
      current: src, // 当前显示图片的http链接
      urls: imgList // 需要预览的图片http链接列表
    })
  },
  /**
   * 拨打电话(联系人)
   */
  call_link: function () {
    if (myUtils.isMobile(this.data.datas.contactName)){
      wx.makePhoneCall({
        phoneNumber: this.data.datas.contactName,
        success: function () {
        },
        fail: function () {
          wx.showToast({
            title: '拨打失败',
            icon:"none"
          })
        }
      })
    }
  },
  /**
   * 拨打电话
   */
  call: function () {
    if (this.data.isCall){
      wx.makePhoneCall({
        phoneNumber: this.data.datas.phone,
        success: function () {
        },
        fail: function () {
          wx.showToast({
            title: '拨打失败',
            icon: "none"
          })
        }
      })
    }else{
      wx.showModal({
        title: '提示',
        content: '请下载服装基地APP拨打电话',
        showCancel:false
      })
    }
    
  },
  //审核中
  toBuyCheck: function (e) {
    wx.showToast({
      title: '审核中',
      icon: 'none',
    })
  },
  /**
   * 求购成功
   */
  toBuySucc: function (e) {
    var that = this;
    httpsUtils.getData({
      _url: app.globalData._url + '/wantBuy/selectSuccess?id=' + that.data.wanBuyId,
      data: {},
    }, function (json) {
      if (json.success) {
        wx.showToast({
          title: '求货成功',
        })
        that.setData({
          wantStatus: 2
        })
      } else {
        wx.showToast({
          title: '失败，请重试',
          icon: 'none',
        })
      }
    })
  },

  /**
   * 求购失败
   */
  toBuyFail: function (e) {
    // wx.showModal({
    //   title: '提示',
    //   content: '请下载服装基地APP后删除求购',
    //   showCancel: false
    // })
    var that = this;
    httpsUtils.getData({
      _url: app.globalData._url + '/wantBuy/del?id=' + that.data.wanBuyId,
      data: {},
    }, function (json) {
      if (json.success) {
        wx.navigateBack({
          delta: 1,
          success: function () {
            wx.showToast({
              title: '删除成功',
            })
          }
        })
      } else {
        wx.showToast({
          title: '失败，请重试',
          icon: 'none',
        })
      }
    })
  },
  /**
   * 获取数据
   */
  getWantData: function () {
    // wx.showLoading({
    //   title: '加载中...',
    // })
    wx.showToast({
      title: '加载中...',
      icon:'loading'
    })
    var that = this;
    httpsUtils.getData({
      _url: app.globalData._url + '/wantBuy/selectOne?id=' + that.data.wanBuyId,
      data: {},
    }, function (json) {
      if (json.data && json.success) {
        var imgUrls = [];
        imgUrls.push(json.data.coverUrl);
        if (json.data.goodsUrl != null) {
          var urls = json.data.goodsUrl.split("***");
          for(let obj of urls){
            imgUrls.push(obj);
          }
        }
          

        var isCall = true;
        var phone = json.data.phone;
        var userType = wx.getStorageSync("userInfo").userType;
        if (json.data.isHidden == 1){
          phone = myUtils.hideStr(json.data.phone, 3, 8, '*');
          isCall = false;
        }
        // else if (userType == 1){
        //   phone = myUtils.hideStr(json.data.phone, 3, 8, '*');
        //   isCall = false;
        // }
        that.setData({
          detailListImg: imgUrls,
          datas: json.data,
          userData: json.data.user,
          wantStatus: json.data.status,
          phone: phone,
          isCall: isCall
        })
      }
      wx.hideToast();
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var isMy = options.isMy == 'true' ? true : false;
    that.setData({
      isMy: isMy,
      wanBuyId:options.id
    })

    //获取数据
    that.getWantData();
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '这款谁家有？点开联系我',
      path: 'pages/askToBuyDetail/askToBuyDetail?isMy=false&id=' + this.data.wanBuyId,
    }
  },

})